#include "DestructibleBox.hpp"
#include "UserData.hpp"
#include <boost/bind.hpp>
#include "Utils.hpp"
#include "Map.hpp"

DestructibleBox::DestructibleBox (Map& map, const int x, const int y,const unsigned int index):
Box(map,x,y,index),
m_life(NORMAL_BOX)
{
	ImagePtr image = LOAD_IMAGE("data/images/box.png");
	if(image.get() != NULL)
		this->SetImage(*image);
	// create user data
	UserData *ud = new UserData();
	ud->type = "box";
	ud->callback = boost::bind(&DestructibleBox::hasBeenHit, this, _1);	

	m_body->SetUserData(static_cast<void*>(ud));
}

DestructibleBox::~DestructibleBox ()
{
}

void DestructibleBox::hasBeenHit(const std::string a)
{
	if(a == "bullet")
		m_life += 1;
	
	if(m_life == DESTROYED_BOX)
		m_map.removeBoxAt(m_index);
		
	else
		this->SetImage(*LOAD_IMAGE("data/images/box_" + numberToString(m_life) + ".png"));	
}
